package Data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;


//import presentation.MainWindow;

import Data.Database;
import Domain.Book;
import Domain.Seller;

/**
 * A database mapper for {@link domain.Book} objects.
 * 
 * @author Daniel Fagerberg
 * 
 */
import Main.Constants;

public class BookMapper extends Constants {

	/**
	 * Inserts a new book into the database. The new book is given as a
	 * {@link domain.Book} object. During the insert operation a new id value
	 * will be created by the Derby database, using auto increment. The newly
	 * created id value is stored in the given {@link domain.Book} object.
	 * 
	 * @param b
	 *            the {@link domain.Book} object holding the book values of
	 *            the new book.
	 * @return the given {@link domain.Book} object.
	 * @throws SQLException
	 *             when the SQL statement cannot be executed.
	 * @throws ClassNotFoundException
	 *             when there is something wrong with the database driver.
	 */

	public Book insert(Book b) throws SQLException, ClassNotFoundException {
		String query = INSERT_BOOK;
		Statement stmt = Database.createStatement();

		query = query.replaceAll("::title", "" + b.getTitle());
		query = query.replaceAll("::isbn", "" + b.getIsbn());
		query = query.replaceAll("::author", "" + b.getAuthor());
		query = query.replaceAll("::edition", "" + b.getEdition());
		query = query.replaceAll("::condition", "" + b.getCondition());
		query = query.replaceAll("::subject", "" + b.getSubject());
		query = query.replaceAll("::price", "" + b.getPrice());
		query = query.replaceAll("::date",
				"" + createSqlDateFormat(b.getDateCreated()));
		System.out.println(createSqlDateFormat(b.getDateCreated()));
		query = query.replaceAll("::issold", "" + b.getIsSold());
		query = query.replaceAll("::id_seller", "" + b.getSeller().getId());

		System.out.println("Calling database:" + query);

		stmt.execute(query, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = stmt.getGeneratedKeys();
		rs.next();
		int generatedId = rs.getInt(1);
		System.out.println("New primary key was generated: " + generatedId);
		b.setId(generatedId);

		return b;
	}

	private String createSqlDateFormat(Date dateCreated) {
		SimpleDateFormat formatter = new SimpleDateFormat(
				Constants.SQL_DATE_FORMAT);
		return formatter.format(dateCreated);
	}

	/**
	 * Retrieves the seller with given primary key.
	 * 
	 * @param id
	 *            an integer that is matches against the primary key field 'id'
	 *            in the database.
	 * @return a {@link domain.Product} object that represents the product with
	 *         given id value in the database.
	 * @throws SQLException
	 *             when the SQL statement cannot be executed.
	 * @throws ClassNotFoundException
	 *             when there is something wrong with the database driver.
	 */
	public Book retrieve(int id) throws SQLException, ClassNotFoundException {
		Statement stmt = Database.createStatement();
		String selectString = SELECT_BOOK;
		selectString = selectString.replaceAll("::id", "" + id);
		System.out.println("Calling database for a book: " + selectString);
		ResultSet rs = stmt.executeQuery(selectString);
		if (rs.next()) {
			SellerMapper sellerMapper = new SellerMapper();
			Seller seller = sellerMapper.retrieve(id);
			Book book = new Book();
			book.setId(id);
			book.setTitle(rs.getString(1));
			book.setAuthor(rs.getString(2));
			book.setIsbn(rs.getString(3));
			book.setEdition(rs.getString(4));
			book.setCondition(rs.getString(5));
			book.setSubject(rs.getString(6));
			book.setPrice(rs.getDouble(7));
			book.setDateCreated(rs.getDate(8));
			book.setIsSold(rs.getInt(9));
			book.setSeller(seller);
			return book;
		} else {
			return null;
		}
	}
	/**
	 * Deletes the given book from the database. The deletion uses the field
	 * id of the given {@link domain.Product} object to identify the correct
	 * database entry, remaining fields of the product object are ignored.
	 * 
	 * @param b
	 *            the project object that will be deleted from the database.
	 * @throws SQLException
	 *             when the SQL statement cannot be executed.
	 * @throws ClassNotFoundException
	 *             when there is something wrong with the database driver.
	 */
	public void delete(Book b) throws SQLException, ClassNotFoundException {
		Statement stmt1 = Database.createStatement();
		String deleteString = DELETE_BOOK;
		deleteString = deleteString.replaceAll("::id", "" + b.getId());
		stmt1.executeUpdate(deleteString);
	}
}
